﻿#if UNITY_EDITOR
using System.IO;
using UnityEditor;
/// <summary>
/// 二进制资源
/// </summary>
public class ESF_BuildBinaryAsset : AbsESF_EditorBuildAsset
{
    #region 构造函数
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    /// <param name="_directory">目录</param>
    /// <param name="_ext">后缀enFileExt</param>
    /// <param name="_bytes">数据流</param>
    public ESF_BuildBinaryAsset(string _fileName, string _directory,
        enESF_UnityFileExt _ext, byte[] _bytes)
        : base(_fileName, _directory, _ext)
    {
        SetBinary(_bytes);
    }
    #endregion

    #region bytes 属性        
    /// <summary>
    /// 流内容
    /// </summary>
    public byte[] bytes { get; private set; }
    #endregion

    #region SetBinary 设置数据流
    /// <summary>
    /// 设置数据流
    /// </summary>
    /// <param name="_bytes">数据流</param>
    public void SetBinary(byte[] _bytes)
    {
        bytes = _bytes;
    }
    #endregion

    #region OnCreateAsset 创建资源
    /// <summary>
    /// 创建资源
    /// </summary>
    protected override void OnCreateAsset()
    {
        File.WriteAllBytes(filePath, bytes);
        switch (ext)
        {
            case enESF_UnityFileExt.UnityPackage:
                AssetDatabase.ImportPackage(filePath, false);
                File.Delete(filePath);
                break;
        }
    }
    #endregion

    #region OnLoadAsset 加载资源
    /// <summary>
    /// 加载资源
    /// </summary>
    protected override void OnLoadAsset()
    {
        bytes = File.ReadAllBytes(filePath);
    }
    #endregion
}
#endif